Public Class frmTongHopTHCBBDaCapChoCacDonVi
    Private _baocao As String
    'phan biet BaoCaoTongGiaTriDaCapChoCacDonVi va BaoCaoTongGiaTriDaNhapChoCacDonVi

    Public Sub New(ByVal baocao As String)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        _baocao = baocao
        If (baocao = "BaoCaoTongGiaTriDaCapChoCacDonVi") Then
            Label6.Text = " TỔNG HỢP VẬT TƯ ĐÃ CẤP CHO CÁC ĐƠN VỊ"
        ElseIf (baocao = "BaoCaoTongGiaTriDaNhapChoCacDonVi") Then
            Label6.Text = " TỔNG HỢP VẬT TƯ ĐÃ NHẬP TỪ CÁC ĐƠN VỊ"
        End If
    End Sub

    Dim loai_kho As String = ""

    Private Sub frmTongHopTHCBBDaCapChoCacDonVi_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        dtpTuNgay.Text = "01/01/" + CStr(Now.Year)
        dtpDenNgay.Text = Now.Date.ToString
    End Sub

#Region "txtLoaiKho"
    Private Sub txtLoaiKho_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtLoaiKho.DoubleClick
        HienDMLoaiKho()
    End Sub

    Private Sub txtLoaiKho_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtLoaiKho.KeyDown
        If (e.KeyCode = Keys.Enter) Or (e.KeyCode = Keys.F1) Then
            HienDMLoaiKho()
        ElseIf (e.Modifiers = Keys.Control) And (e.KeyCode = Keys.Delete) Then
            loai_kho = ""
            txtLoaiKho.Text = ""
        End If
    End Sub

    Public Sub HienDMLoaiKho()
        Dim strDMLoaiKho As String
        strDMLoaiKho = "select KH,TEN from tblDMKhac where L = 'L'"
        Dim ma, ten As String
        ma = ""
        ten = ""
        If (txtLoaiKho.Text <> "") Then
            Dim strSql As String
            strSql = "select TEN from tblDMKhac where L = 'L' and KH = '" & txtLoaiKho.Text & "'"
            Dim ten_loaikho As String
            ten_loaikho = BaseDB.ExecSql_DataValue(strSql)
            If (Not ten_loaikho Is Nothing) Then
                loai_kho = txtLoaiKho.Text
                txtLoaiKho.Text = ten_loaikho
            Else
                'hien DM LoaiKho
                HienDM(strDMLoaiKho, "TEN", "KH", ten, ma)
                loai_kho = ma
                txtLoaiKho.Text = ten
            End If
        Else
            'hien DM LoaiKho
            HienDM(strDMLoaiKho, "TEN", "KH", ten, ma)
            loai_kho = ma
            txtLoaiKho.Text = ten
        End If
    End Sub

    Public Sub HienDM(ByVal strsql As String, ByVal strDM As String, ByVal strVM As String, _
                ByRef DM As String, ByRef VM As String)
        Dim frm As New frmChonDanhMucTheoListBox(strsql, strDM, strVM, "")
        frm.ShowDialog()
        DM = frm.DisplayMember
        VM = frm.ValueMember
    End Sub
#End Region

    Private Sub btnBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBaoCao.Click
        Dim tungay, denngay As String
        Dim thoigian As String
        tungay = CStr(dtpTuNgay.Value.Month) + "/" + CStr(dtpTuNgay.Value.Day) + "/" + CStr(dtpTuNgay.Value.Year)
        denngay = CStr(dtpDenNgay.Value.Month) + "/" + CStr(dtpDenNgay.Value.Day) + "/" + CStr(dtpDenNgay.Value.Year)
        thoigian = "Từ ngày " + dtpTuNgay.Text + " đến ngày " + dtpDenNgay.Text
        '
        Dim strsql As String
        Dim ds As DataSet
        strsql = "select * from viewTongHopHangXuatChoLLTT "

        If (_baocao = "BaoCaoTongGiaTriDaCapChoCacDonVi") Then
            strsql &= "where NX = 'X' "
        ElseIf (_baocao = "BaoCaoTongGiaTriDaNhapChoCacDonVi") Then
            strsql &= "where NX = 'N' "
        End If

        strsql &= " and (NGAY >= '" + tungay + "' and NGAY <= '" + denngay + "')"
        If (loai_kho <> "") Then
            strsql &= " and lk = '" & loai_kho & "'"
        End If
        ds = BaseDB.ExecSql_DataSet(strsql)
        Dim nnreport As Object
        If (loai_kho = "1") Then
            nnreport = New rptBaoCaoTongGiaTriDaCap
            nnreport.SetDataSource(ds.Tables(0))
            If (_baocao = "BaoCaoTongGiaTriDaCapChoCacDonVi") Then
                nnreport.SetParameterValue("tieude", "Đã Cấp Các Đơn Vị")
                nnreport.SetParameterValue("thoigian", thoigian)
            ElseIf (_baocao = "BaoCaoTongGiaTriDaNhapChoCacDonVi") Then
                nnreport.SetParameterValue("tieude", "Đã Nhập Từ Các Đơn Vị")
                nnreport.SetParameterValue("thoigian", thoigian)
            End If
        Else
            nnreport = New rptBaoCaoTongGiaTriTHCBBDaCap
            nnreport.SetDataSource(ds.Tables(0))
            nnreport.SetParameterValue("loai_kho", txtLoaiKho.Text)
            If (_baocao = "BaoCaoTongGiaTriDaCapChoCacDonVi") Then
                nnreport.SetParameterValue("tieude", "Đã Cấp Các Đơn Vị")
                nnreport.SetParameterValue("thoigian", thoigian)
            ElseIf (_baocao = "BaoCaoTongGiaTriDaNhapChoCacDonVi") Then
                nnreport.SetParameterValue("tieude", "Đã Nhập Từ Các Đơn Vị")
                nnreport.SetParameterValue("thoigian", thoigian)
            End If
        End If

        Dim frm As New frmReportViewer(nnreport)
        frm.ShowDialog()
    End Sub
End Class